package com.ustudy.tools.ui.Runner;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Browser extends Application
{
    public static void main( String[] args )
    {
        launch(args);
    }

	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("简易浏览器");
		BorderPane borderPane=new BorderPane();
		borderPane.setTop(this.createTop());
		borderPane.setCenter(this.createCenter());
		primaryStage.setScene(new Scene(borderPane));
		primaryStage.setHeight(768);
		primaryStage.setWidth(1024);
		primaryStage.setResizable(false);
		primaryStage.show();
	}
	
	public Node createTop() {
		HBox hBox=new HBox();
		hBox.setPadding(new Insets(15));
		hBox.setSpacing(5);
		
		final ComboBox<String> comboBox=new ComboBox<>();
		comboBox.getItems().addAll("http","https");
		comboBox.setValue("http");
		hBox.getChildren().add(comboBox);
		Text t=new Text("://");
		t.setFont(new Font(16));
		hBox.getChildren().add(t);
		final TextField textField=new TextField();
		textField.setMinWidth(600);
		hBox.getChildren().add(textField);
		Button b=new Button();
		b.setText("访问");
		hBox.getChildren().add(b);
		
		b.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e)->{
			String url=textField.getText();
			webView.getEngine().load(comboBox.getValue()+"://"+url);
		});
		
		Button prev=new Button("后退");
		prev.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e)->{
			WebHistory history=webView.getEngine().getHistory();
			if(history.getCurrentIndex()>0) {
				history.go(-1);
			}
		});
		hBox.getChildren().add(prev);
		
		Button next=new Button("前进");
		next.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e)->{
			WebHistory history=webView.getEngine().getHistory();
			if(history.getCurrentIndex()<history.getMaxSize()) {
				history.go(1);
			}
		});
		hBox.getChildren().add(next);
		return hBox;
	}
	private WebView webView;
	public Node createCenter() {
		this.webView=new WebView();
		return webView;
	}
}
